const puppeteer = require("puppeteer");
const express = require("express");
const cors = require("cors");

const app = express();

app.use(cors());

app.get("/test", (req, res) => {
  res.send("测试成功");
});

app.get("/getmodile", async (req, res) => {
   const res5 = await data
  res.send(res5);
});

app.listen(3000, () => {
  console.log("http://localhost:3000");
});

const data = new Promise((reslove, reject) => {
  jd();
  async function jd() {
    const brower = await puppeteer.launch();
    const page = await brower.newPage();
    await page.setViewport({
      width: 2000,
      height: 800,
    });
    await page.goto("https://www.jd.com/");

    const input = await page.$("#key");
    await input.type("手机");
    // 输入框回车
    await input.press("Enter");

    // 等待页面元素加载完毕
    await page.waitForSelector("ul.gl-warp>li.gl-item");
    const list = await page.$$eval("ul.gl-warp>li.gl-item", (ele) =>
      ele.map((item) => item.innerText)
    );
    let changeList = list.map((item) => {
      item = item.replaceAll("\n", "");
      return item;
    });
    reslove(changeList);
  }
})
